package org.study.event.test.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.study.event.core.transaction.TranactionListenerManager;
import org.study.event.test.mapper.GoodMapper;
import org.study.event.test.po.Good;
import org.study.event.test.service.GoodService;
import org.study.event.test.vo.Message;

@Service
public class GoodServiceImpl implements GoodService {

	@Autowired
	private GoodMapper goodMapper;

	@Autowired
	private TranactionListenerManager tranactionListenerManager;

	/**
	 * 保存商品信息
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveGood(Good good) {

		/***
		 * 先查询商品名称是否存在
		 */

		if (good != null) {
			goodMapper.saveGood(good);
			tranactionListenerManager.pushTransactionEvent(generateInsertMessage(good));
		}

	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveOtherGood(Good good) {

		/***
		 * 先查询商品名称是否存在
		 */

		if (good != null) {
			goodMapper.saveGood(good);
			tranactionListenerManager.pushTransactionEvent(generateInsertMessage(good));
		}

	}

	private Message generateInsertMessage(Good... goods) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String time = format.format(new Date());
		StringBuilder builder = new StringBuilder();

		for (int index = 0; index < goods.length; index++) {
			Good good = goods[index];
			builder.append(time + " 保存商品信息 ");
			builder.append("{商品名称:").append(good.getGoodName());
			builder.append(",商品价格:").append(good.getPrice());
			builder.append(",商品数量:").append(good.getGoodCount());
			builder.append("}");
			if (index != goods.length - 1) {
				builder.append(",");
			}
		}

		Message message = new Message();
		message.setContext(builder.toString());
		message.setModel(1);
		message.setTime(time);
		return message;
	}

	/***
	 * 批量执行操作
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public void batchSaveGood(List<Good> goodList) {
		if (goodList != null && goodList.size() > 0) {
			goodMapper.saveGoodList(goodList);
			// 记录业务日志发生消息操作
			Good[] goods = new Good[goodList.size()];
			goods = goodList.toArray(goods);
			tranactionListenerManager.pushTransactionEvent(generateInsertMessage(goods));
		}
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public List<Good> findGoodByGoodIds(List<Long> goodIds) {
		return goodMapper.findByGoodIds(goodIds, true);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public void updateGoodStock(Long goodId, Integer saleCount) {
		goodMapper.saleGoodCount(goodId, saleCount);
	}

}
